package com.sgb.member.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.SparseArray;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.google.android.material.tabs.TabLayout;
import com.sgb.member.MemberConfig;
import com.sgb.member.R;
import com.sgb.member.databinding.ActivityMemberDemandBinding;
import com.sgb.member.ui.fragment.CollectiveJoinFragment;
import com.sgb.member.ui.fragment.CollectiveQuotationFragment;
import com.swgk.core.base.AppActivity;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.customview.XCustomTabLayout;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.recyclerview.adapter.SlidingFragmentPagerAdapter;
import com.swgk.core.util.MToast;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;

/**
 * 集采报价
 */
public class CollectiveQuotationActivity extends AppActivity {
    private ActivityMemberDemandBinding mBinding;
    private ArrayList<Fragment> mFragments;

    public static void start(Context context) {
        Intent intent = new Intent(context, CollectiveQuotationActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_member_demand);
        initSearchView();
        getCollectingType();
    }

    // 集采分类 1,需方集采，2企业专项
    public void getCollectingType() {
        List<CommonPopEntity> entityList = new ArrayList<>();
        // groupBuyType 1即报集采 2 汇总集采
        entityList.add(new CommonPopEntity("1", "需方发起"));
        entityList.add(new CommonPopEntity("2", "企业专项"));
        initTabLayout(entityList);
    }

    private void initTabLayout(List<CommonPopEntity> data) {
        mBinding.emptyView.setVisibility(View.GONE);
        mFragments = new ArrayList<>();
        String[] titles = new String[data.size()];
        for (int i = 0; i < data.size(); i++) {
            mFragments.add(CollectiveQuotationFragment.newInstance(i, data.get(i)));
            titles[i] = data.get(i).getTypeName();
        }
        SlidingFragmentPagerAdapter adapter = new SlidingFragmentPagerAdapter(getSupportFragmentManager(), mFragments, titles);
        mBinding.vp.setOffscreenPageLimit(titles.length);
        mBinding.vp.setAdapter(adapter);
        mBinding.tabLayout.setupWithViewPager(mBinding.vp);
        mBinding.tabLayout.setTabMode(TabLayout.MODE_FIXED);
        mBinding.tabLayout.setNormalColor(R.color.color_main_bg);
        mBinding.tabLayout.setSelectColor(R.color.color_white);
        mBinding.tabLayout.setNormalSize(15);
        mBinding.tabLayout.setSelectSize(18);
        mBinding.tabLayout.addOnTabSelectedListener(data, new XCustomTabLayout.SimpleSelectedListener(){
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mBinding.vp.setCurrentItem(tab.getPosition());
            }
        });
    }

    private String mSearchText = ""; // 搜索内容
    private final SparseArray<String> mSearchList = new SparseArray<>();

    private void initSearchView() {
        mBinding.activityBar.ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        String hint = "请输入关键词，比如采购内容、企业名称等";
        mBinding.activityBar.etView.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        mBinding.activityBar.etView.setHint(hint);
        mBinding.activityBar.etView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (TextUtils.isEmpty(mBinding.activityBar.etView.getText().toString())) {
                        MToast.showToast(getActivity(), hint);
                        return true;
                    }
                    if (mSearchText.equals(mBinding.activityBar.etView.getText().toString())) {
                        MToast.showToast(getActivity(), "请输入不同的搜索内容");
                        return true;
                    }
                    mSearchText = mBinding.activityBar.etView.getText().toString().trim();
                    loadSearchData();
                }
                return false;
            }
        });
        mBinding.activityBar.etView.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (TextUtils.isEmpty(s) || s.length() == 0) {
                    mBinding.activityBar.tvSearchDelete.setVisibility(View.INVISIBLE);
                } else {
                    mBinding.activityBar.tvSearchDelete.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mBinding.activityBar.tvSearchDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSearchText = "";
                mBinding.activityBar.etView.setText("");
                loadSearchData();
            }
        });
        mBinding.activityBar.tvSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(mSearchText)) {
                    if (TextUtils.isEmpty(mBinding.activityBar.etView.getText().toString())) {
                        MToast.showToast(getActivity(), hint);
                        return;
                    }
                }
                if (mSearchText.equals(mBinding.activityBar.etView.getText().toString())) {
                    MToast.showToast(getActivity(), "请输入不同的搜索内容");
                    return;
                }
                mSearchText = mBinding.activityBar.etView.getText().toString().trim();
                loadSearchData();
            }
        });
        mBinding.activityBar.tvSearchDelete.setVisibility(View.INVISIBLE);
    }

    public String getSearchText() {
        int tabPosition = mBinding.tabLayout.getSelectedTabPosition();
        return mSearchList.get(tabPosition);
    }

    private void loadSearchData() {
        int tabPosition = mBinding.tabLayout.getSelectedTabPosition();
        mSearchList.put(tabPosition, mSearchText);
        CollectiveQuotationFragment fragment = (CollectiveQuotationFragment) mFragments.get(tabPosition);
        fragment.loadData(false);
    }
}
